package com.jmxcfc.blfsc.jiwei.controller;

import com.alibaba.fastjson.JSONObject;
import com.jmxcfc.blfsc.jiwei.service.ISmsNotifyRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 极微短信通知回调控制器
 * </p>
 * @author pbs
 */
@RestController
@RequestMapping("/jiwei")
@Slf4j
public class JiWeiNotifyController {
    private ISmsNotifyRequestService smsNotifyRequestService;


    @Autowired
    public void setSmsNotifyRequestService(ISmsNotifyRequestService smsNotifyRequestService) {
        this.smsNotifyRequestService = smsNotifyRequestService;
    }

    /**
     * 集微短信回调通知入口:
     * 接收状态报告端返回 ok 两个字母（不区分大小写），为正常接收到了推送的状态报告;
     若返回其他值任意值，则为接收推送的状态报告失败。我方对于接收失败的会进行三次的重新推送。
     * @param notify
     * @return
     */
    @RequestMapping("/sms/notify")
    public String notify(@RequestBody JSONObject notify) {
        try {
            log.info("集微短信状态回调通知,接收到数据:{}", notify);
            String respMsg = smsNotifyRequestService.notifyHandler(notify);
            log.info("集微短信状态回调通知,处理结束,响应结果:{}", respMsg);
            return respMsg;
        } catch (Exception e) {
            log.info("集微短信回调通知处理异常,参数:{},异常信息:{}", notify,e.getMessage(),e);
            return "ERROR";
        }
    }
}
